package com.briup.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.briup.bean.User;
import com.briup.bean.UserExtend;
import com.briup.service.UserService;
import com.briup.util.MD5Util;
import com.briup.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @BelongsProject: cms
 * @BelongsPackage: com.briup.web.controller
 * @Author: li
 * @CreateTime: 2024-11-20  09:54
 * @Description: TODO
 * @Version: 1.0
 */
@Api(tags = "用户模块")
@Slf4j
@RestController
@RequestMapping("/auth/user")
public class UserController {
    @Autowired
    UserService userService;

    //admin使用：用户登录后首页展示个人信息需要使用
    @ApiOperation("获取用户个人信息")
    @GetMapping("/info")
    public Result getInfo(@RequestAttribute("userId") Long id)
    {
        log.info("id:{}",id);
        return Result.success(userService.queryById(id));
    }

    @ApiOperation(value = "新增用户", notes = "username、password必须存在不为空，且username唯一")
    @PostMapping("/save")
    public Result save(@RequestBody User user){
        userService.insert(user);
        return Result.success("新增用户成功");
    }

    @ApiOperation(value = "查询用户")
    @GetMapping("/query")
    public Result query(Integer pageNum, Integer pageSize, String
            username, String status, Integer roleId, Integer isVip){
        IPage<UserExtend> p = userService.query(pageNum, pageSize,
                username, status, roleId, isVip);
        return Result.success(p);
    }

    @ApiOperation(value = "通过id查询")
    @GetMapping("queryById/{id}")
    public Result queryById(@PathVariable("id") Long id){
        User user = userService.queryById(id);
        return Result.success(user);
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping("/deleteByBatch/{ids}")
    public Result deleteByBatch(@PathVariable("ids")List<Long> ids){
        userService.deleteByBatch(ids);
        return Result.success("删除成功");
    }

    @ApiOperation(value = "更新用户信息")
    @PutMapping("/update")
    public Result update(@RequestBody User user){
        userService.myUpdate(user);
        return Result.success("更新完成");
    }

    @ApiOperation(value = "设置用户为Vip", notes = "id存在且有效")
    @PutMapping("/setVip/{id}")
    public Result setVip(@PathVariable Long id) {
        userService.setVip(id);
        return Result.success("更新成功");
    }
}
